package org.zoomdy.gdbserver.extender;

public class GDBServerExtender
{
    public static void main(String[] args)
    {
        System.out.println("欢迎使用GDBServer Extender 0.0.2");
        System.out.print("该工具为GDBServer扩展功能以支持RTOS的多线程调试等特性，");
        System.out.print("当前版本支持Cortex-M架构的eCos多线程调试及插入Ctrl+C控制符，");
        System.out.print("当前版本是非常早期的原型版本，长远目标是创建Eclipse插件，或基于Eclipse RCP的独立工具，");
        System.out.println("访问项目主页http://gdbx.zoomdy.org查看最新版本。");
        System.out.print("该工具由zoomdy编写，可以通过邮箱mingdu.zheng@gmail.com向他汇报bug或反馈使用情况，");
        System.out.println("访问他的博客http://blog.zoomdy.org查找GDBServer Extender的使用说明。");
        
        if(GDBServerExtender.debugEnable)
        {
            System.out.println("");
            System.out.println("调试版本，将打印与GDBServer的通信数据包。");
        }
        
        Head head = new Head();
        head.setProperty("listenPort", "10000");
        
        Tail tail = new Tail();
        tail.setProperty("connectHost", "127.0.0.1");
        tail.setProperty("connectPort", "2331");
        
        eCos ecos = new eCos();
        CtrlC ctrlC = new CtrlC();
        FixJLink fixJLink = new FixJLink();
        
        if(GDBServerExtender.debugEnable)
        {
            ecos.setProperty("verbose", "true");
            
            Logger logger1 = new Logger();
            logger1.setProperty("id", "1:");
            Logger logger2 = new Logger();
            logger2.setProperty("id", "2:");
            
            head.setNext(logger1);
            logger1.setNext(ecos);
            ecos.setNext(fixJLink);
            fixJLink.setNext(ctrlC);
            ctrlC.setNext(logger2);
            logger2.setNext(tail);
            tail.setNext(null);
            
            tail.setPrev(ctrlC);
            ctrlC.setPrev(logger2);
            logger2.setPrev(fixJLink);
            fixJLink.setPrev(ecos);
            ecos.setPrev(logger1);
            logger1.setPrev(head);
            head.setPrev(null);
        }
        else
        {
            head.setNext(ecos);
            ecos.setNext(fixJLink);
            fixJLink.setNext(ctrlC);
            ctrlC.setNext(tail);
            tail.setNext(null);
            
            tail.setPrev(ctrlC);
            ctrlC.setPrev(fixJLink);
            fixJLink.setPrev(ecos);
            ecos.setPrev(head);
            head.setPrev(null);
        }
        
        INode node;
        node = head;
        while(node != null)
        {
            boolean ret = node.start();
            if(ret == false)
            {
                node = node.getPrev();
                while(node != null)
                {
                    node.stop();
                    node = node.getPrev();
                }
                System.exit(0);
                break;
            }
            
            node = node.getNext();
        }
    }
    
    public static boolean debugEnable = false;
}
